   package com.javalong.affairs.module.task.fragment;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.module.notifything.activity.DeptListActivity;
import com.javalong.affairs.base.activity.MessageSearchedActivity;
import com.javalong.affairs.module.notifything.activity.NotificationActivity;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.Constant;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.Dept;
import com.javalong.affairs.base.ui.TextSelectView;
import com.javalong.affairs.base.ui.TextSelectView.OnImageClick;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.DateTimePickDialogUtil;
import com.javalong.affairs.base.utils.DateUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class TaskSearchFragment extends BaseFragment implements
		OnItemSelectedListener, OnClickListener {
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.tv_starttime)
	private TextView tv_starttime;
	@ViewInject(R.id.tv_endtime)
	private TextView tv_endtime;
	@ViewInject(R.id.ui_tsview_dept)
	private TextSelectView tsView_dept;
	@ViewInject(R.id.et_keyword)
	private EditText et_keyword;
	@ViewInject(R.id.bt_search)
	private Button bt_search;
	@ViewInject(R.id.sp_type)
	private Spinner sp_type;
	@ViewInject(R.id.bt_starttime)
	private Button bt_starttime;
	@ViewInject(R.id.bt_endtime)
	private Button bt_endtime;
	@ViewInject(R.id.ll_type)
	private LinearLayout ll_type;
	private Date nowDate = new Date();
	private List<Dept> deptList;
	private int currType;
	private DateTimePickDialogUtil startDialog;
	private DateTimePickDialogUtil endDialog;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tasksearch, container,
				false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		ViewUtils.inject(this);
		bt_starttime.setOnClickListener(this);
		bt_endtime.setOnClickListener(this);
		bt_search.setOnClickListener(this);
		tsView_dept.setOnImageClick(new OnImageClick() {
			@Override
			public void onClick(View view) {
				IntentManager.doActivityForResult(getActivity(),
						DeptListActivity.class, RequestCode.DEPTLIST);
			}
		});
		initSpinner();
		if (getArguments() != null) {
			boolean isSchedule = getArguments().getBoolean(
					BundleKey.ISSCHEDULE, false);
			if (isSchedule) {
				currType = 2;
				ll_type.setVisibility(View.GONE);
			}
		}
		registerEvent(new int[] { R.string.event_add_dept });

		startDialog = new DateTimePickDialogUtil(getActivity(),
				DateUtils.formatDate(nowDate));
		endDialog = new DateTimePickDialogUtil(getActivity(),
				DateUtils.formatDate(nowDate));
		Activity activity = getActivity();
		if(activity instanceof NotificationActivity){
			((NotificationActivity) getActivity()).topTitle.setTitle("消息查询");
		}
		
	}

	private void initSpinner() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, new String[] { "接收消息",
						"发送消息", "个人日程" });
		sp_type.setAdapter(adapter);
		sp_type.setOnItemSelectedListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_starttime:
			startDialog.dateTimePicKDialog(tv_starttime);
			break;
		case R.id.bt_endtime:
			endDialog.dateTimePicKDialog(tv_endtime);
			break;
		case R.id.bt_search:
			Map<String, Object> params = new HashMap<String, Object>();
			if (startDialog.getSeconds() != 0) {
				params.put(RequestKey.TIMEBEGIN, startDialog.getSeconds());
			}
			if (endDialog.getSeconds() != 0) {
				params.put(RequestKey.TIMEEND, endDialog.getSeconds());
			}
			if (!StringUtils.isEmpty(et_keyword.getText().toString())) {
				params.put(RequestKey.KEYWORDS, et_keyword.getText().toString());
			}
			if (deptList != null && deptList.size() > 0) {
				StringBuilder sb = new StringBuilder();
				for (Dept d : deptList) {
					sb.append(d.id).append(Constant.CONNECT_SINGAL);
				}
				sb.deleteCharAt(sb.length() - 1);
				params.put(RequestKey.SECTION, sb.toString());
			}
			Bundle bundle = new Bundle();
			bundle.putSerializable(BundleKey.REQUESTPARAMS,
					(Serializable) params);
			bundle.putInt(BundleKey.SEARCHTYPE, currType);
			IntentManager.doActivity(getActivity(),
					MessageSearchedActivity.class, bundle);
			break;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		currType = position;
		switch (position) {
		case 0:
			break;
		case 1:
			break;
		case 2:
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}

	@Override
	public void OnSynCalled(int ulID, Bundle bundle) {
		super.OnSynCalled(ulID, bundle);
		switch (ulID) {
		case R.string.event_add_dept:
			deptList = (List<Dept>) bundle.getSerializable(BundleKey.DEPTLIST);
			StringBuilder sb = new StringBuilder();
			for (Dept d : deptList) {
				sb.append(d.name).append(Constant.CONNECT_SINGAL);
			}
			sb.deleteCharAt(sb.length() - 1);
			tsView_dept.setText(sb.toString());
			break;
		}
	}
}
